package com.jprocms.module.cms.controller.admin.content.vo;

import cn.hutool.core.text.CharSequenceUtil;
import com.jpro.framework.common.util.json.JsonUtils;
import com.mzt.logapi.starter.annotation.DiffLogField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.jsoup.Jsoup;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 内容 Base VO，提供给添加、修改、详细的子 VO 使用
 * 如果子 VO 存在差异的字段，请不要添加到这里，影响 Swagger 文档生成
 */
@Data
public class ContentBaseVO {

    @Schema(description = "栏目ID", required = true)
    @DiffLogField(name = "栏目ID")
    private Long channelId;

    @Schema(description = "引用栏目ID")
    @DiffLogField(name = "引用栏目ID")
    private Set<Long> subChannelIds;


    @Schema(description = "栏目名称")
    @DiffLogField(name = "栏目名称")
    private String channelName;

    @Schema(description = "内容状态(1:已发布;   2:流转中;   3:待发布;  4:退回;  5:下线  6:草稿;   7 删除 ")
    @DiffLogField(name = "内容状态")
    private Integer status;

    @Schema(description = "内容标题", required = true)
    @DiffLogField(name = "内容标题")
    @NotNull(message = "内容标题不能为空")
    private String title;

    @Schema(description = "发布时间")
    @DiffLogField(name = "发布时间")
    private Date releaseTime;

    @Schema(description = "置顶级别 0非置顶 越大排序靠前")
    @DiffLogField(name = "置顶级别")
    private Integer topLevel;

    @Schema(description = "站点ID")
    private Long siteId;

    @Schema(description = "模型ID")
    private Long modelId;

    @Schema(description = "原文ID")
    private Long originContentId;

    @Schema(description = "创建方式（1:直接创建    2:投稿  3:引用  4:复制   5  主动采集")
    private Integer createMode;

    @Schema(description = "创建人ID")
    private Long userId;

    @Schema(description = "标记属性")
    @DiffLogField(name = "标记属性")
    private Long dictMark;

    @Schema(description = "区块ID")
    @DiffLogField(name = "区块ID")
    private Long dictBlock;

    @Schema(description = "tag词 逗号,拼接")
    @DiffLogField(name = "tag词")
    private String tagNames;

//    @Schema(description =  "扩展表对象")
//    private ContentExtUpdateReqVO ext;

    /**************************下面是ext表属性****************************************/

    @Schema(description = "预约发布时间")
    @DiffLogField(name = "预约发布时间")
    private Date onlineTime;

    @Schema(description = "下线时间")
    @DiffLogField(name = "下线时间")
    private Date offlineTime;

    @Schema(description = "置顶开始时间")
    @DiffLogField(name = "置顶开始时间")
    private Date topStartTime;

    @Schema(description = "置顶结束时间")
    @DiffLogField(name = "置顶结束时间")
    private Date topEndTime;

    @Schema(description = "副标题")
    @DiffLogField(name = "副标题")
    private String subtitle;

    @Schema(description = "外部链接地址")
    @DiffLogField(name = "外部链接地址")
    private String outLink;

    @Schema(description = "是否新窗口打开")
    @DiffLogField(name = "是否新窗口打开")
    private Boolean targetBlank;

    @Schema(description = "seo关键词")
    @DiffLogField(name = "seo关键词")
    private String keywords;

    @Schema(description = "摘要")
    @DiffLogField(name = "摘要")
    private String description;

    @Schema(description = "作者")
    @DiffLogField(name = "作者")
    private String author;

    @Schema(description = "来源")
    @DiffLogField(name = "来源")
    private String source;

    @Schema(description = "系统图片")
    @DiffLogField(name = "系统图片")
    private String sysImage;

    @Schema(description = "系统视频")
    @DiffLogField(name = "系统视频")
    private String sysVideo;

    @Schema(description = "视频时长")
    @DiffLogField(name = "视频时长")
    private Integer sysVideoDuration;

    @Schema(description = "系统音频")
    @DiffLogField(name = "系统音频")
    private String sysAudio;

    @Schema(description = "系统附件")
    @DiffLogField(name = "系统附件")
    private String sysAttach;

    @Schema(description = "系统附件名称")
    @DiffLogField(name = "系统附件名称")
    private String sysAttachName;

    @Schema(description = "文库")
    @DiffLogField(name = "文库")
    private String docPdf;

    @Schema(description = "文库原始文档")
    @DiffLogField(name = "文库原始文档")
    private String originDoc;

    @Schema(description = "文库文档名称")
    @DiffLogField(name = "文库文档名称")
    private String docName;

    @Schema(description = "pc模板")
    @DiffLogField(name = "pc模板")
    private String pcTpl;

    @Schema(description = "h5模板")
    @DiffLogField(name = "h5模板")
    private String h5Tpl;

    @Schema(description = "流程实例ID")
    @DiffLogField(name = "流程实例ID")
    private String processInstanceId;

    @Schema(description = "退回原因")
    @DiffLogField(name = "退回原因")
    private String rejectReason;

    @Schema(description = "系统默认正文")
    @DiffLogField(name = "系统默认正文")
    private String text;
    @Schema(description = "多图json")
    private String imagesJson;
    @Schema(description = "附件json")
    private String attachsJson;

    @Schema(description = "图片集JSON")
    private List<ContentImageVo> images;

    @Schema(description = "文件集JSON")
    private List<ContentFileVo> attachs;

    /****************下面是attr表属性********************************************************/

    @Schema(description = "自定义字段")
    @DiffLogField(name = "自定义字段")
    private Map<String, Object> customs;


    @Schema(description = "是否下载远程图片替换")
    private Boolean downRemoteImage;

    @Schema(description = "标题是否加粗")
    private Boolean titleBold;

    @Schema(description = "副标题是否加粗")
    private Boolean subtitleBold;

    @Schema(description = "编辑器类型(1:wangeditor富文本编辑器,2:tinymce编辑器)")
    private Integer editorType = 1;

    @Schema(description = "搜索权重")
    private Integer searchWeight = 0;

    @Schema(description = "纯文本格式的系统默认正文")
    public String getPlainText() {
        return Optional.ofNullable(getText())
                .map(html -> Jsoup.parse(html).body().text())
                // 多个空格替换为一个空格，中文空格unicode为 \u3000
                .map(text -> text.replaceAll("[ \\u3000]+", " "))
                .map(String::trim)
                .orElse(CharSequenceUtil.EMPTY);
    }

    /**
     * 传递参数vo传递images 集合 入库之前  转换序列化字符串
     *
     * @param images
     */
    public void setImages(List<ContentImageVo> images) {
        this.images = images;
        this.imagesJson = JsonUtils.toJsonString(images);
    }

    /**
     * 传递参数vo传递attachs 集合 入库之前  转换序列化字符串
     *
     * @param images
     */
    public void setAttachs(List<ContentFileVo> attachs) {
        this.attachs = attachs;
        this.attachsJson = JsonUtils.toJsonString(attachs);
    }

}
